import java.util.*;
import java.io.*;

public class UserManager {
    private List<User> users = new ArrayList<>();
    private static final String USER_FILE = "users.txt";

    public UserManager() {
        loadUsersFromFile();
        // 如果文件为空，初始化一个admin
        if (users.isEmpty()) {
            users.add(new Admin("1", "admin", "ynuinfo#777.", "12345678901"));
            saveUsersToFile();
        }
    }

    // 从文件加载用户数据
    private void loadUsersFromFile() {
        try (BufferedReader br = new BufferedReader(new FileReader(USER_FILE))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length >= 5) {
                    User user = null;
                    switch (parts[4]) {
                        case "admin":
                            user = new Admin(parts[0], parts[1], parts[2], parts[3]);
                            break;
                        case "manager":
                            user = new Manager(parts[0], parts[1], parts[2], parts[3]);
                            break;
                        case "frontdesk":
                            user = new FrontDesk(parts[0], parts[1], parts[2], parts[3]);
                            break;
                    }
                    if (user != null) {
                        users.add(user);
                    }
                }
            }
        } catch (IOException e) {
            System.out.println("用户数据文件不存在，将创建新文件");
        }
    }

    // 保存用户数据到文件
    private void saveUsersToFile() {
        try (PrintWriter pw = new PrintWriter(new FileWriter(USER_FILE))) {
            for (User user : users) {
                pw.println(user.getId() + "," + user.getUsername() + "," +
                        user.getPassword() + "," + user.getPhone() + "," + user.getType());
            }
        } catch (IOException e) {
            System.out.println("保存用户数据失败：" + e.getMessage());
        }
    }

    public User login(String username, String password) {
        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

    public boolean addUser(User user) {
        for (User u : users) {
            if (u.getUsername().equals(user.getUsername()) || u.getId().equals(user.getId())) {
                return false;
            }
        }
        users.add(user);
        saveUsersToFile(); // 保存到文件
        return true;
    }

    public boolean removeUser(String username) {
        Iterator<User> it = users.iterator();
        while (it.hasNext()) {
            User u = it.next();
            if (u.getUsername().equals(username) && !u.getType().equals("admin")) {
                it.remove();
                saveUsersToFile(); // 保存到文件
                return true;
            }
        }
        return false;
    }

    public User getUserByUsername(String username) {
        for (User u : users) {
            if (u.getUsername().equals(username)) return u;
        }
        return null;
    }

    public List<User> getAllUsers() {
        return users;
    }

    // 更新用户信息并保存
    public void updateUser(User user) {
        saveUsersToFile();
    }
}